<template>
  <a-modal
    :title="modalTile"
    style="top: 20px"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display: none">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="字典名称">
        <a-input
          placeholder="字典名称"
          v-decorator="['dictName', { rules: [{ required: true, message: '请输入字典名称' }] }]"
        />
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="字典类型">
        <a-input
          v-decorator="['dictType', { rules: [{ required: true, message: '请输入字典类型' }] }]"
          placeholder="字典类型"
        />
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
        <a-select v-decorator="['status', { initialValue: '0', rules: [{ required: true, message: '请选择状态' }] }]">
          <a-select-option :value="'0'">正常</a-select-option>
          <a-select-option :value="'1'">禁用</a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
        <TextArea :rows="4" placeholder="备注" v-decorator="['remark']" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveDictType } from '@/api/system/sysDict'
import TextArea from '@/components/TextArea'
import pick from 'lodash.pick'
export default {
  name: 'DictModal',
  props: {},
  components: {TextArea},
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      roleAll: [],
      mdl: {},
      form: this.$form.createForm(this),
      modalTile: undefined,
    }
  },
  beforeCreate() {},
  created() {},
  methods: {
    add() {
      this.modalTile = '添加字典类型'
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    edit(record) {
      if (record.id) {
        this.modalTile = '编辑字典类型'
      }
      this.mdl = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'dictName', 'dictType', 'status', 'remark'))
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveDictType(values)
            .then((res) => {
              if (res.code === 200) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.success(res.message)
              }
            })
            .catch(() => {
              this.$message.error('系统错误，请稍后再试')
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  },
}
</script>
